package dbexp.model;

import static dbexp.model.db.DbOT.TRIGGER;
import static dbexp.model.db.DbOT.TRIGGERS;

import java.util.ArrayList;

public class Trigger extends SourceType {

	/**
	 *TESTTRIGGER
	 */
	private static final long serialVersionUID = 1L;
	private boolean forEachRow = false;
	private boolean insert = false;
	private boolean delete = false;
	private boolean update = false;
	public static final String LABEL = "trigery";
	public static final String DEF_ICON = "icon-event";
	public static final long BEFORE = 0;
	public static final long AFTER = 1;

	public Trigger() {
		super(LABEL, TRIGGERS);
		setIcons(DEF_ICON);
	}

	public static Trigger instantOf(SourceType st) {
		Trigger object = new Trigger(st.getName(), st.getSchema());
		object.setErrors(st.getErrors());
		object.setLinies(st.getLinie());
		object.setWrapped(st.isWrapped());
		return object;
	}

	public Trigger(String triggerName) {
		super(triggerName, TRIGGER);
		setIcons(DEF_ICON);
		set("leaf", true);
	}

	public Trigger(String triggerName, String schema) {
		this(triggerName);
		setSchema(schema);
	}

	public String getName() {
		return super.getName();
	}

	public String getSchema() {
		return super.getSchema();
	}

	public boolean isForEachRow() {
		return forEachRow;
	}

	public void setForEachRow(boolean forEachRow) {
		this.forEachRow = forEachRow;
	}

	public long getWhen() {
		return getInt("when", 0);
	}

	public void setWhen(long when) {
		set("when", when);
	}

	public boolean isInsert() {
		return insert;
	}

	public void setInsert(boolean insert) {
		this.insert = insert;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

	public boolean isUpdate() {
		return update;
	}

	public void setUpdate(boolean update) {
		this.update = update;
	}

	public ArrayList<String> getLinie() {
		return super.getLinie();
	}

	public ArrayList<Error> getErrors() {
		return super.getErrors();
	}

	public String getErrorString() {
		String out = " ";
		for (int i = 0; i < getBledy().size(); i++) {
			out += getErrors().get(i).getText();
		}
		return out;
	}

	public String when() {
		String out = "";
		if (getWhen() == AFTER) {
			out = " AFTER";
		} else {
			out = " BEFORE";
		}
		return out;
	}

	public String event() {
		String out = "";
		if (insert) {
			out = " OR INSERT";
		}
		if (delete) {
			out += " OR DELETE";
		}
		if (update) {
			out += " OR UPDATE";
		}
		out = out.replaceFirst(" OR", "");
		return out;
	}

	public String getContent() {
		String out = " ";
		for (int i = 0; i < getLinie().size(); i++) {
			out += getLine(i);
		}
		return out;
	}

	private String getQuery(String oper) {
		if (requiredFieldIsNotNull()) {
			String query = "TRIGGER " + getObjectNameForQuery();
			return oper + query + "\n" + when() + "\n" + event() + "\n"
					+ getContent();
		} else {
			return oneFromDual();
		}
	}

	public String getQuery(int operation) {
		if (operation == CREATE) {
			return getQuery("CREATE OR REPLACE ");
		} else if (operation == ALTER) {
			return "ALTER TRIGGER " + getObjectNameForQuery() + " COMPILE";
		} else if (operation == DELETE) {
			return "DROP TRIGGER " + getObjectNameForQuery();
		}
		return oneFromDual();
	}
}
